package Admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dushibao.admin.AdminApplication;
import com.dushibao.admin.model.Goods;
import com.dushibao.admin.service.IGoodsService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 *  商品 测试类
 *
 * @author zqd
 *
 * @date 2021-12-10 10:39:13
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdminApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GoodsServiceDemo {
    
    @Autowired
    IGoodsService goodsService;

    @Test
    public void getById(){
        Goods goods = this.goodsService.getById(1);
        Assert.assertNotNull(goods);
    }

    @Test
    public void list(){
        List<Goods> goodss = this.goodsService.list();
        goodss.forEach(System.out::println);
    }

    @Test
    public void save(){
        Goods goods = new Goods();
        goods.setGoodsName("商品名称");
        goods.setGoodsSalenum(100);
        goods.setDeleteStatus(0);
        goods.setGoodsPrice(new BigDecimal("10000"));

        for(int i=0;i<100;i++){
            this.goodsService.save(goods);
        }
    }

    @Test
    public void removeById(){
        boolean ret = this.goodsService.removeById(1L);
        Assert.assertEquals(true,ret);
    }

    @Test
    public void saveOrUpdate(){
//        Goods goods = this.goodsService.getById(1);
//        boolean ret = this.goodsService.saveOrUpdate(goods);
//        Assert.assertEquals(true,ret);

        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("password","1234567");

        this.goodsService.update(updateWrapper);

    }

    @Test
    public void page(){
        Integer currentPage = 1;
        Integer pageSize = 20;

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        IPage<Goods> iPage = new Page<>(currentPage,pageSize);
        IPage<Goods> page = this.goodsService.page(iPage,queryWrapper);

        page.getRecords().forEach(System.out::println);

    }

}
